using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DialogManager : MonoBehaviour
{
    public Text nameText;
    public Text dialogueText;
    public float TypingSpeed=0.02f;

    private Queue<string> Sentences;
    private Queue<string> Names;

    void Start()
    {
        Sentences = new Queue<string>();
        Names = new Queue<string>();
    }

    public void StartDialog(Dialog dialog)
    {
        //Debug.Log("Starting conversation with " + dialog.name);
        nameText.text = dialog.name;
        Sentences.Clear();
        foreach (string sentence in dialog.sentences)
        {
            Sentences.Enqueue(sentence);
        }
        DisplayNextSentence();
    }
    public void DisplayNextSentence()
    {
        if(Sentences.Count == 0) 
        {
            EndDialogue();
            return;
        }

        string sentnce = Sentences.Dequeue();
        //Debug.Log(sentnce);
        StopAllCoroutines();
        StartCoroutine(TypeSentence(sentnce));
    }

    IEnumerator TypeSentence(string sentence)
    {
        dialogueText.text = "";
        foreach (char letter in sentence.ToCharArray())
        {
            dialogueText.text += letter;
            yield return new WaitForSeconds(TypingSpeed);
        }
    }
    public void EndDialogue()
    {
        Debug.Log("End of the conversation");
    }
    
}
